package com.hqyj.controller;

import com.hqyj.dao.HouseDao;
import com.hqyj.entity.House;

import com.hqyj.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @Author: cj
 * @Date: 2020/9/1 9:52
 * @Version: 1.0
 */
@Controller
@RequestMapping("/restful")
public class HouseController {

    @Autowired
    HouseDao houseDao;

    @GetMapping
    @RequestMapping(value = "/houses")
    String houses(Model m) {
        List<House> houses = houseDao.getAll();
        m.addAttribute("houses", houses);
        return "restful/houseList";
    }

    @GetMapping
    @RequestMapping(value = "/house1")
    String houses1(Model m,Integer pageNum){
        System.out.println("121221222221"+pageNum);
        if (pageNum == null){
            pageNum = 1;
        }
//        PageRequest pageReques=PageRequest.of(pageNum-1,3);   //获取第1页的两条记录
//        Sort sort = Sort.by(Sort.Direction.DESC, "house_id");
        Pageable pageable = PageRequest.of(pageNum-1,4);
        Page<House> page=houseDao.findAll(pageable);
//        List<House> houses=page.getContent();
//        for(House t:houses) {
//            System.out.println(t);
//            houses.add(t);
//        }
//        m.addAttribute("pageInfo", houses);

        m.addAttribute("houses",page);

        System.out.println("3333332222"+page);
        return "restful/houseList";
    }

//    @GetMapping
//    @RequestMapping(value = "/housefind")
//    String houses2(Model m,String houseaddress) {
//        List<House> houses = houseDao.findByHouseaddressLike(houseaddress);
//        m.addAttribute("houses", houses);
//        return "restful/houseList";
//    }
/*    @GetMapping
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    String updateHouse(String houseaddress, String housetype,String housesize) {
        int num = houseDao.save(houseaddress, housetype,housesize);
        System.out.println("添加的条数=" + num);
        return "redirect:houses";

    }*/

    @GetMapping
    @RequestMapping(value = "/house")
    String add() {
        return "restful/form_add";
    }



    @RequestMapping(value = "/house",method = RequestMethod.POST)
    String add_do(Model m,String housename, String houseaddress, String housetype, String housesize , String houseprice, String housestate, Integer user_id, @RequestParam("file") MultipartFile file) {
//        if ((file.getOriginalFilename().isEmpty())) {
//            return "文件上传失败，请选择文件";
//        } else {
//            // 获取图片的名字
//            String fileName = file.getOriginalFilename();
//            String lastName=fileName.substring(fileName.indexOf("."),fileName.length());//获取尾缀 如.png .jpg
//            String filePath=
//            // 图片存放的文件夹地址
//
////            String filePath= ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/";
//            // 文件夹地址 + 名字 = 图片的路径
//            File dest=new File(filePath+fileName);
////            House house=new House();
////            house.setJpg("http://localhost:8080/"+fileName);
////            String jpg = house.getJpg();
//            String jpg = filePath + fileName;
//            try {
//                file.transferTo(dest);
//                System.out.println("上传成功！");
//                return "上传成功";
//            } catch (IOException e) {
//                System.out.println("上传失败"+e.toString());
//            }
//            try {
//                // 图片上传的工具类
//                // 参数一：图片的编码格式的字节数组
//                // 参数二：图片存放的文件夹地址
//                // 参数三：图片的名字
//                FileUtils.uploadFile(file.getBytes(), filePath, fileName);
//
//                // 把图片路径写入数据库
////                head.setHeadAddress(fileAddress);
////                addHeadAddress.insert(head);
//
//            } catch (Exception e) {
//            }


        if (StringUtils.isEmpty(file.getOriginalFilename())){
            System.out.println("文件为空");
        }
        String upload = FileUpload.upload(file);


        int num = houseDao.add(housename, houseaddress, housetype, housesize, houseprice, housestate, user_id,upload);
            System.out.println("添加的条数=" + num);
            return "redirect:house1";
        }


    @RequestMapping(value = "/house/{id}",method = RequestMethod.GET)
    String update(@PathVariable String id, Model m) {
        House house = houseDao.getOne(Integer.parseInt(id));
        m.addAttribute("house",house);
        return "restful/form_update";
    }

    @GetMapping
    @RequestMapping(value = "/house/{id}",method = RequestMethod.PUT)
    String updateHouse(@PathVariable Integer id, House house) {
        house.setHouse_id(id);
        int num = houseDao.updateById(house);
        return "redirect:../house1";
    }

    @RequestMapping(value = "/house/{id}")
    @ResponseBody
    String delete(@PathVariable int id) {
        houseDao.deleteById(id);
        return "ok";
    }
}
